﻿using System;
using BoldBrick.Core.Logging;
using BoldBrick.OneIntranet.HR.DataObjects;
using Microsoft.SharePoint;
using BoldBrick.SharePoint.CommonControls.Extensions;
using Microsoft.SharePoint.Utilities;

namespace BoldBrick.OneIntranet.HR.Utils
{
	public static class PositionsHelper
	{
		private static readonly ILogger log = Logger.GetLogger(typeof(PositionsHelper));
		
		/// <summary>
		/// Closes a position
		/// </summary>
		/// <param name="item">Item of the position to close</param>
		public static void ClosePosition(SPListItem item)
		{
			log.Write(LogLevel.Info, "Closing position: {0} (ID={1})", item.Title, item.ID);
			if (IsClosedOnSet(item))
			{
				log.Write(LogLevel.Info, "Position is already closed: {0} (ID={1})", item.Title, item.ID);
				return;
			}
			
			item[SiteColumns.ClosedOnColumn.InternalName] = DateTime.Today;
			item.Update();
			log.Write(LogLevel.Info, "Position has been closed: {0} (ID={1})", item.Title, item.ID);
		}

		/// <summary>
		/// Opens a position
		/// </summary>
		/// <param name="item">Item of the position to open</param>
		public static void OpenPosition(SPListItem item)
		{
			log.Write(LogLevel.Info, "Opening position: {0} (ID={1})", item.Title, item.ID);
			if (!IsClosedOnSet(item))
			{
				log.Write(LogLevel.Info, "Position is already opened: {0} (ID={1})", item.Title, item.ID);
				return;
			}
			
			item[SiteColumns.ClosedOnColumn.InternalName] = null;
			item.Update();
			log.Write(LogLevel.Info, "Position has been opened: {0} (ID={1})", item.Title, item.ID);
		}

		/// <summary>
		/// Creats a copy of a position
		/// </summary>
		/// <param name="item">Item of the position to copy</param>
		/// <returns>Returns ID of created position item if item is created, otherwise returns null</returns>
		public static int? CopyPosition(SPListItem item)
		{
			SPList list = item.ParentList;
			log.Write(LogLevel.Info, "Copying position: {0} (ID={1})", item.Title, item.ID);
			string urlDirectory = SPUtility.GetUrlDirectory(item.Url);
			SPFolder folder = list.ParentWeb.GetFolder(urlDirectory);

			int itemIDNew = item.Copy(list, folder, item.Title, false);
			SPListItem itemNew = list.GetItemById(itemIDNew);

			if (itemNew != null)
			{
				// set new properties
				itemNew[SiteColumns.OpenFromColumn.InternalName] = DateTime.Today;
				itemNew[SiteColumns.ClosedOnColumn.InternalName] = null;

				itemNew.Update();

				log.Write(LogLevel.Info, "Position has beed copied: {0} (ID={1})", itemNew.Title, itemNew.ID);
				return itemIDNew;
			}

			return null;
		}

		/// <summary>
		/// Gets true if "ClosedOn" field is set to a value, otherwise returns false
		/// </summary>
		/// <param name="item">Position item</param>
		/// <returns>Returns true if "ClosedOn" field is set to a value, otherwise returns false</returns>
		public static bool IsClosedOnSet(SPListItem item)
		{
			DateTime? dateClosedOn = item.GetValue<DateTime>(SiteColumns.ClosedOnColumn.InternalName);
			return dateClosedOn.HasValue;
		}
	}
}
